<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Group;
use Illuminate\Http\Request;

class GroupController extends Controller {


    //列表
    public function index(){
        $userId = auth('api')->id();
        $params = [
            'uid'    => $userId,
        ];
        $lists = Group::getList(1000,$params,['id','name']);
        return $this->writeJson(0,$lists);
    }

    //更新
    public function update(Request $request)
    {
        $data = $request->all();
        $data['user_id'] = auth('api')->id();
        if($data['id']){
            $info = Group::where('user_id',$data['user_id'])->findOrFail($data['id']);
            $info->update($data);
            return $this->writeJson(0,null,'修改成功');
        }else{
            unset($data['id']);
            Group::create($data);
            return $this->writeJson(0,null,'新增成功');
        }
    }


    //删除
    public function destroy(Request $request){
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        Group::where('user_id',auth('api')->id())->where('id',$id)->delete();
        return $this->writeJson(0);
    }

}
